

	/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}baseline_for figures.dta"
	CREATES:	${out_tables}/T37A_mediation_sem.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

	macro  def Xvar1 "femalechild age2-age6"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
    macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  
	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"


/*******************************************************************************
Table A37: Mediation analysis: Socioemotional Index
*******************************************************************************/
	* Rename relevant variables
	*---------------------------------

	
	gen femalechild=(gender==2)
	tab age_year, g(age)


	xtset CENTRO_SALUD

	g TIPOB=TIPO if TIPO!=3
	g TIPOI=TIPO if TIPO!=2		



	rename zptevir_irtscore2 zpt_irts2
	rename zibatt_score z_batt
*--------------------------------
* Impacts on Mediators 
*--------------------------------

local intermediate "zhome_score_gsem znur_score_gsem zdisc_score_gsem1b zdisc_score_gsem2b zPSCS zpacotis zPSSS_friends"	

		foreach var of local intermediate {				
				xi: xtreg `var' i.TIPO  if x==1, fe i(CENTRO_SALUD)
				estimates store `var'				
				
			}
	


local finaloutcomes "zpt_irts2 zsoc_index" 
local intermediate "zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS zpacotis zPSSS_friends "	

rename zdisc_score_gsem1b zdiscneg_gsem
rename zdisc_score_gsem2b zdiscpos_gsem

*-------------------------------------
* Mediation with Individual Indicators
*-------------------------------------

foreach var of local finaloutcomes {
	foreach med_var of local intermediate {

				xi: xtreg `var' i.TIPO `med_var' $Xvar1, fe i(CENTRO_SALUD)
				estimates store `var'_`med_var'					
			}
		xi: xtreg `var' i.TIPO $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med0		
		xi: xtreg `var' i.TIPO zhome_score_gsem $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med1		
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med2	
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem zdiscpos_gsem $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med3
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med4
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS zpacotis $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med5
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS zpacotis zPSSS_friends $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med6
	    xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS zpacotis zPSSS_friends zdiscneg_gsem zIAP_authoritarian zIAP_authoritative zIAP_permissive zPSSS_family zPSSS_others zPSI_Distress zCESD $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med7
		xi: xtreg `var' i.TIPO zhome_score_gsem znur_score_gsem $Xvar1, fe i(CENTRO_SALUD)
		estimates store `var'_med8

}


	    esttab zsoc_index_med0 zsoc_index_zhome_score_gsem zsoc_index_znur_score_gsem zsoc_index_zdiscpos_gsem zsoc_index_zPSCS zsoc_index_zpacotis zsoc_index_zPSSS_friends  zsoc_index_med6	///	
		using "$out_tables/appendix/T37A_mediation_sem.csv", replace f ///
		cells("b(fmt(3)star)" "se(fmt(3)par)") ///
		star(* 0.10 ** 0.05 *** 0.01) ///
        stats(r2 N, fmt(2  0 ) labels("R-squared"  "Observations")) legend  ///
		keep(_ITIPO_2 _ITIPO_3 zhome_score_gsem znur_score_gsem zdiscpos_gsem zPSCS zpacotis zPSSS_friends ) /* 
		*/ mtitles("Base" "Home Index" "PBC Nurturing Index	" "PBC Positive Discipline" "Perceived Self-efficacy""PACOTIS""Perceived Friends Support""Significant Mediators") /*
		*/ varlabels(_ITIPO_2 "NEP-B" _ITIPO_3 "NEP-I" zhome_score_gsem  "Home Index" znur_score_gsem  "PBC Nurturing Index" zdiscpos_gsem "PBC Positive Discipline Index"  zPSCS  "Perceived Self-efficacy" zpacotis  "NPACOTIS" zPSSS_friends  "Perceived Friends Support" )
		
		
